const nodeExternals = require('webpack-node-externals');
const webpack = require('webpack');
const globals = require('./global-vars.json');
const HardSource = require('hard-source-webpack-plugin');
const { co } = require('c2h6o');

const plugins = [
  new webpack.DefinePlugin(globals),
  new webpack.ProvidePlugin({
    Vue: ['vue-property-decorator', 'Vue']
    // TLOG: ['@/api', 'tagLog']
  }),
  new HardSource()
];

const resolveLoader = {
  modules: ['node_modules', 'loaders']
};

function chainWebpack(config) {
  loaders(config.module);
}
function loaders(module) {


  module.rule('toml').test(/\.toml$/)
    .use('toml-loader')
    .loader('toml-loader').end();

}

const mapConfig = {
  get development() {
    return {
      lintOnSave: false,
      // 所有 webpack-dev-server 的选项 都支持
      devServer: {
        port: 8181
      },
      configureWebpack: {
        resolveLoader,
        plugins
      },
      chainWebpack,
    }
  },
  get production() {
    return {
      lintOnSave: false,
      publicPath: './',
      outputDir: 'dist',
      configureWebpack: {
        resolveLoader,
        // target: 'node',
        entry: './src/components/index.ts',
        // name: 'uhv_plug',
        externals: [nodeExternals()],
        output: {
          libraryTarget: 'commonjs2',
          pathinfo: true,
        },
        plugins
      },
      chainWebpack,
    };
  }
}

const env = process.env.NODE_ENV;
const config = mapConfig[env];

if (!config) {
  co.error(`No config found for env: ${env}!`, true);
}

module.exports = config;
